<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python documentation Index' />
<link rel="first" href="lib.html" title='Python library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="module-getopt.html" />
<link rel="prev" href="module-time.html" />
<link rel="parent" href="allos.html" />
<link rel="next" href="optparse-background.html" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name='aesop' content='information' />
<title>14.3 optparse -- More powerful command line option parser</title>
</head>
<body>
<div class="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="14.2 time  "
  href="module-time.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="14. generic Operating System"
  href="allos.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="14.3.1 Background"
  href="optparse-background.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-time.html">14.2 time  </a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="allos.html">14. Generic Operating System</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="optparse-background.html">14.3.1 Background</a>
</div>
<hr /></div>
</div>
<!--End of Navigation Panel-->

<h1><a name="SECTION0016300000000000000000">
14.3 <tt class="module">optparse</tt> -- More powerful command line option parser</a>
</h1>
<a name="module-optparse"></a>

<span class="versionnote">New in version 2.3.</span>

<p>
<code>optparse</code> is a more convenient, flexible, and powerful library for
parsing command-line options than <code>getopt</code>.  <code>optparse</code> uses a more
declarative style of command-line parsing: you create an instance of
<tt class="class">OptionParser</tt>, populate it with options, and parse the command line.
<code>optparse</code> allows users to specify options in the conventional GNU/POSIX
syntax, and additionally generates usage and help messages for you.

<p>
Here's an example of using <code>optparse</code> in a simple script:
<div class="verbatim"><pre>
from optparse import OptionParser
[...]
parser = OptionParser()
parser.add_option("-f", "--file", dest="filename",
                  help="write report to FILE", metavar="FILE")
parser.add_option("-q", "--quiet",
                  action="store_false", dest="verbose", default=True,
                  help="don't print status messages to stdout")

(options, args) = parser.parse_args()
</pre></div>

<p>
With these few lines of code, users of your script can now do the
``usual thing'' on the command-line, for example:
<div class="verbatim"><pre>
&lt;yourscript&gt; --file=outfile -q
</pre></div>

<p>
As it parses the command line, <code>optparse</code> sets attributes of the
<code>options</code> object returned by <tt class="method">parse_args()</tt> based on user-supplied
command-line values.  When <tt class="method">parse_args()</tt> returns from parsing this
command line, <code>options.filename</code> will be <code>"outfile"</code> and
<code>options.verbose</code> will be <code>False</code>.  <code>optparse</code> supports both long
and short options, allows short options to be merged together, and
allows options to be associated with their arguments in a variety of
ways.  Thus, the following command lines are all equivalent to the above
example:
<div class="verbatim"><pre>
&lt;yourscript&gt; -f outfile --quiet
&lt;yourscript&gt; --quiet --file outfile
&lt;yourscript&gt; -q -foutfile
&lt;yourscript&gt; -qfoutfile
</pre></div>

<p>
Additionally, users can run one of
<div class="verbatim"><pre>
&lt;yourscript&gt; -h
&lt;yourscript&gt; --help
</pre></div>

<p>
and <code>optparse</code> will print out a brief summary of your script's
options:
<div class="verbatim"><pre>
usage: &lt;yourscript&gt; [options]

options:
  -h, --help            show this help message and exit
  -f FILE, --file=FILE  write report to FILE
  -q, --quiet           don't print status messages to stdout
</pre></div>

<p>
where the value of <em>yourscript</em> is determined at runtime (normally
from <code>sys.argv[0]</code>).

<p>

<p><br /></p><hr class='online-navigation' />
<div class='online-navigation'>
<!--Table of Child-Links-->
<a name="CHILD_LINKS"><strong>Subsections</strong></a>

<ul class="ChildLinks">
<li><a href="optparse-background.html">14.3.1 Background</a>
<ul>
<li><a href="optparse-terminology.html">14.3.1.1 Terminology</a>
<li><a href="optparse-what-options-for.html">14.3.1.2 What are options for?</a>
<li><a href="optparse-what-positional-arguments-for.html">14.3.1.3 What are positional arguments for?</a>
</ul>
<li><a href="optparse-tutorial.html">14.3.2 Tutorial</a>
<ul>
<li><a href="optparse-understanding-option-actions.html">14.3.2.1 Understanding option actions</a>
<li><a href="optparse-store-action.html">14.3.2.2 The store action</a>
<li><a href="optparse-handling-boolean-options.html">14.3.2.3 Handling boolean (flag) options</a>
<li><a href="optparse-other-actions.html">14.3.2.4 Other actions</a>
<li><a href="optparse-default-values.html">14.3.2.5 Default values</a>
<li><a href="optparse-generating-help.html">14.3.2.6 Generating help</a>
<li><a href="optparse-printing-version-string.html">14.3.2.7 Printing a version string</a>
<li><a href="optparse-how-optparse-handles-errors.html">14.3.2.8 How <tt class="module">optparse</tt> handles errors</a>
<li><a href="optparse-putting-it-all-together.html">14.3.2.9 Putting it all together</a>
</ul>
<li><a href="optparse-reference-guide.html">14.3.3 Reference Guide</a>
<ul>
<li><a href="optparse-creating-parser.html">14.3.3.1 Creating the parser</a>
<li><a href="optparse-populating-parser.html">14.3.3.2 Populating the parser</a>
<li><a href="optparse-defining-options.html">14.3.3.3 Defining options</a>
<li><a href="optparse-standard-option-actions.html">14.3.3.4 Standard option actions</a>
<li><a href="optparse-option-attributes.html">14.3.3.5 Option attributes</a>
<li><a href="optparse-standard-option-types.html">14.3.3.6 Standard option types</a>
<li><a href="optparse-parsing-arguments.html">14.3.3.7 Parsing arguments</a>
<li><a href="optparse-querying-manipulating-option-parser.html">14.3.3.8 Querying and manipulating your option parser</a>
<li><a href="optparse-conflicts-between-options.html">14.3.3.9 Conflicts between options</a>
<li><a href="optparse-cleanup.html">14.3.3.10 Cleanup</a>
<li><a href="optparse-other-methods.html">14.3.3.11 Other methods</a>
</ul>
<li><a href="optparse-option-callbacks.html">14.3.4 Option Callbacks</a>
<ul>
<li><a href="optparse-defining-callback-option.html">14.3.4.1 Defining a callback option</a>
<li><a href="optparse-how-callbacks-called.html">14.3.4.2 How callbacks are called</a>
<li><a href="optparse-raising-errors-in-callback.html">14.3.4.3 Raising errors in a callback</a>
<li><a href="optparse-callback-example-1.html">14.3.4.4 Callback example 1: trivial callback</a>
<li><a href="optparse-callback-example-2.html">14.3.4.5 Callback example 2: check option order</a>
<li><a href="optparse-callback-example-3.html">14.3.4.6 Callback example 3: check option order (generalized)</a>
<li><a href="optparse-callback-example-4.html">14.3.4.7 Callback example 4: check arbitrary condition</a>
<li><a href="optparse-callback-example-5.html">14.3.4.8 Callback example 5: fixed arguments</a>
<li><a href="optparse-callback-example-6.html">14.3.4.9 Callback example 6: variable arguments</a>
</ul>
<li><a href="optparse-extending-optparse.html">14.3.5 Extending <tt class="module">optparse</tt></a>
<ul>
<li><a href="optparse-adding-new-types.html">14.3.5.1 Adding new types</a>
<li><a href="optparse-adding-new-actions.html">14.3.5.2 Adding new actions</a>
</ul></ul>
<!--End of Table of Child-Links-->
</div>

<div class="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="14.2 time  "
  href="module-time.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="14. generic Operating System"
  href="allos.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="14.3.1 Background"
  href="optparse-background.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-time.html">14.2 time  </a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="allos.html">14. Generic Operating System</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="optparse-background.html">14.3.1 Background</a>
</div>
</div>
<hr />
<span class="release-info">Release 2.5.1, documentation updated on 18th April, 2007.</span>
</div>
<!--End of Navigation Panel-->
<address>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</address>
</body>
</html>
